# 菜品分类管理
from django.db.models import Q
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
from myadmin.models import Category, Shop
from django.core.paginator import Paginator
from datetime import datetime
import random


# Create your views here.
def index(request, pIndex=1):
    """ 浏览信息 """
    # umod = Category.objects
    clist = Category.objects.filter(status__lt=9)

    # 搜索条件
    mywhere = []
    kw = request.GET.get('keyword', None)
    # print(kw)
    if kw:
        clist = clist.filter(name__contains=kw)
        mywhere.append('keyword=' + kw)

    # 分页处理
    pIndex = int(pIndex)
    page = Paginator(clist, 10)  # 5条数据一页显示
    maxpages = page.num_pages  # 获取最大页数
    # 判断不能越界
    if pIndex > maxpages:
        pIndex = maxpages
    if pIndex < 1:
        pIndex = 1
    list2 = page.page(pIndex)  # 获取当前页数据
    plist = page.page_range  # 获取页码列表信息

    # 遍历当前菜品分类信息并封装对应的店铺名称
    for vo in list2:
        # print(vo)
        sob = Shop.objects.get(id=vo.shop_id)
        vo.shop_name = sob.name
    context = {
        'category_list': list2,
        'plist': plist,
        'pIndex': pIndex,
        'maxpages': maxpages,
        'mywhere': mywhere
    }
    return render(request, 'myadmin/Category/index.html', context)


def add(request):
    """ 添加 """
    # slist = Shop.objects.filter(status__lt=9)
    slist = Shop.objects.filter(status=1)  # 正常店铺才可以加菜品分类
    slist = slist.values('id', 'name')
    context = {'shoplist': slist}
    # print(context)
    return render(request, 'myadmin/category/add.html', context)


from utils.encrypt import md5
from utils.encrypt import add_salt


def insert(request):
    """ 执行添加 """
    print(2)
    try:
        ob = Category()
        ob.shop_id = request.POST['shop_id']
        ob.name = request.POST['name']
        ob.status = 1
        ob.create_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        ob.update_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        ob.save()
        context = {'info': '添加成功'}
    except Exception as arr:
        print(arr)
        context = {'info': '添加失败'}

    return render(request, 'myadmin/info.html', context)


def delete(request, cid=0):
    """ 删除 """
    print(cid)
    try:
        ob = Category.objects.get(id=cid)
        ob.status = 9
        ob.update_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        ob.save()
        context = {'info': '删除成功'}
    except Exception as arr:
        print(arr)
        context = {'info': '删除失败'}

    return render(request, 'myadmin/info.html', context)


def edit(request, cid=0):
    """ 编辑 """
    try:
        slist = Shop.objects.filter(status=1)  # 正常店铺才可以加菜品分类
        slist = slist.values('id', 'name')

        ob = Category.objects.get(id=cid)
        context = {'category': ob, 'shoplist': slist}
        return render(request, 'myadmin/category/edit.html', context)

    except Exception as arr:
        print(arr)
        context = {'info': '没有找到要修改的信息'}
        return render(request, 'myadmin/info.html', context)


def update(request, cid=0):
    """ 执行编辑 """
    print(cid)
    try:
        ob = Category.objects.get(id=cid)
        ob.shop_id = request.POST['shop_id']
        ob.name = request.POST['name']
        ob.status = request.POST['status']
        ob.update_at = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        ob.save()
        context = {'info': '修改成功'}
    except Exception as arr:
        print(arr)
        context = {'info': '修改失败'}

    return render(request, 'myadmin/info.html', context)


def loadCategory(request, sid):
    clist = Category.objects.filter(status__lt=9, shop_id=sid).values('id', 'name')
    # 返回QuerySet对象，使用list强转成对象菜品的分类信息
    return JsonResponse({'data': list(clist)})
